package com.cowain.monitor.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.cowain.monitor.dao.ErpSoDao;
import com.cowain.monitor.dao.PmsErpContentLogDao;
import com.cowain.monitor.dao.PmsOrderInfoDao;
import com.cowain.monitor.datasources.DataSourceNames;
import com.cowain.monitor.datasources.annotation.DataSource;
import com.cowain.monitor.datasources.config.TransactionConfig;
import com.cowain.monitor.entity.ErpSoEntity;
import com.cowain.monitor.entity.PmsOrderInfoEntity;
import com.cowain.monitor.service.ErpSoService;
import com.cowain.monitor.service.PmsOrderInfoService;
import com.cowain.monitor.utils.PmsConstantUtils;
import com.cowain.monitor.utils.PmsProjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 *  
 *  * @author 冬暖
 *  * @projectName pms-monitor
 *  * @title     ErpSoServiceImpl   
 *  * @package    com.cowain.monitor.service.impl  
 *  * @description    ERP销售订单       
 *  * @date   2020/7/27 14:22  
 *  * @version V1.0.0
 *  
 */
@Service
public class ErpSoServiceImpl extends ServiceImpl<ErpSoDao, ErpSoEntity> implements ErpSoService {
    @Autowired
    private ErpSoDao erpSoDao;
    @Autowired
    private PmsOrderInfoDao infoDao;
    @Autowired
    private PmsProjectUtil pmsProjectUtil;
    /**
     * 查询所有的销售订单
     * @return 销售订单列表
     */
    @Override
    @DataSource(name = DataSourceNames.SECOND)
    public  List<ErpSoEntity> findErpSoList(){
        //获取所有的销售订单
        List<ErpSoEntity> erpSoEntities = erpSoDao.findList(PmsConstantUtils.IS_DEL);
        return erpSoEntities;
    }

    /**
     * 插入销售订单数据
     */
    @Override
    @DataSource(name = DataSourceNames.FIRST)
    @Transactional(value = TransactionConfig.DEFAULT_TX,rollbackFor = Exception.class)
    public void insertPmsOrderInfo(List<ErpSoEntity> erpSoEntities,Long type){
        int start = 0;
        //新增数集合
        List<PmsOrderInfoEntity> infoList = new ArrayList<>();
        //修改数据集合
        List<PmsOrderInfoEntity> infoEntityList = new ArrayList<>();

        //遍历查询项目号,项目号查不到就不新增到pms库
        erpSoEntities.forEach(order -> {
            Long id;
            if(order.getProjectNo().contains("-")){
                String projectNo = order.getProjectNo().substring(start,pmsProjectUtil.getFirst()) + "" + order.getProjectNo().substring(pmsProjectUtil.getFirst() + 1);
                id = erpSoDao.findProjectNo(projectNo);
            }else {
                id = erpSoDao.findProjectNo(order.getProjectNo());
            }
            Long projectId = infoDao.findPmsOrderInfo(id,PmsConstantUtils.ORDER_TYPE_INFO);
            PmsOrderInfoEntity info = new PmsOrderInfoEntity();
            info.setProjectId(id);
            info.setOrdNum(order.getDocNo());
            info.setOrdNu(order.getNum().longValue());
            Long code = pmsProjectUtil.findCode(PmsConstantUtils.STATUS_SO, order.getState());
            info.setOrdStatus(code);
            info.setOrdTime(order.getDdate());
            info.setProjectNo(order.getProjectNo());
            info.setOrdType(type);
            if(projectId != null){
                info.setId(projectId);
                infoEntityList.add(info);
            }else {
                infoList.add(info);
            }
            order.setProjectId(id);
        });

        //修改数据集合
        List<PmsOrderInfoEntity> list1 = new ArrayList<>();
        boolean flag = false;

        //新增数据库中无重复数据，
        List<Long> list = pmsProjectUtil.addList(infoList, list1);
        if(infoList.size() > 0 && list.size()> 0){
            List<PmsOrderInfoEntity> list2= new ArrayList<>();
            infoList.forEach(info -> {
                if(info.getProjectId() == null){
                    list2.add(info);
                }
            });
            infoList.removeAll(list2);
            if(infoList.size() > 0){
                List<List<PmsOrderInfoEntity>> split = pmsProjectUtil.split(infoList, 12);
                for (List<PmsOrderInfoEntity> entities : split) {
                    infoDao.insertList(entities);
                }
                flag = true;
            }
        }
        //修改数据库中重复数据
        if(infoEntityList.size() > 0){
            if(list1.size() > 0){
                infoEntityList.addAll(list1);
            }
            List<List<PmsOrderInfoEntity>> split = pmsProjectUtil.split(infoEntityList, 12);
            for (List<PmsOrderInfoEntity> pmsOrderInfoEntities : split) {
                infoDao.updateInfoList(pmsOrderInfoEntities);
            }
        }
        if(list1.size() ==0){
            list1.addAll(infoEntityList);
        }
        if(list.size() > 0 || infoEntityList.size() > 0) {
            List<PmsOrderInfoEntity> infoEntities = new ArrayList<>();
            if(flag) {
                infoEntities.addAll(infoList);
            }
            //添加日志
            pmsProjectUtil.addLog(infoEntities, list1, PmsConstantUtils.ERP_USER, PmsConstantUtils.ERP_USER);
        }
    }

    /**
     * 修改销售订单状态为已读
     */
    @Override
    @DataSource(name = DataSourceNames.SECOND)
    @Transactional(value = TransactionConfig.SECOND_TX,rollbackFor = Exception.class)
    public void updateErpOrder(List<ErpSoEntity> erpSoEntities){
        erpSoEntities.forEach(so -> {
            if(so.getProjectId() != null){
                so.setIsDel(1);
            }else {
                so.setIsDel(0);
            }
        });
        List<ErpSoEntity> collect = erpSoEntities.stream()
                .filter(s -> s.getIsDel() == 1)
                .collect(Collectors.toList());
        List<List<ErpSoEntity>> split = pmsProjectUtil.split(collect, 12);
        for (List<ErpSoEntity> soEntities : split) {
            erpSoDao.updateList(soEntities);
        }
    }
}
